package com.mornd.gulimall.product.web.v1;

import com.mornd.gulimall.common.core.constant.LoginConstant;
import com.mornd.gulimall.common.core.entity.SessionUser;
import com.mornd.gulimall.product.entity.Category;
import com.mornd.gulimall.product.entity.vo.Catalog2Vo;
import com.mornd.gulimall.product.service.ICategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * @author: mornd
 * @dateTime: 2023/8/10 - 14:34
 */

@Controller
public class IndexController {
    @Resource
    private ICategoryService categoryService;

    /**
     * 跳转到主页，并查询所有一级分类
     *
     * @param model
     * @param session spring session 会将 session 包装为 org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryRequestWrapper.HttpSessionWrapper.org.springframework.session.data.redis.RedisIndexedSessionRepository.RedisSession
     * @return
     */
    @GetMapping(value = {"/", "/index.html"})
    public String index(Model model, HttpSession session) {
        List<Category> categorys = categoryService.getLevelCategorys(1);
        model.addAttribute("categorys", categorys);

        // 登录用户信息
//        SessionUser sessionUser = (SessionUser) session.getAttribute(LoginConstant.SESSION_USER);

        return "index";
    }

    /**
     * 查询所有二级和三级分类
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Object getCatalogJson() {
        Map<String, List<Catalog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }
}
